<?php

// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use service\NodeService;
use think\Db;

/**
 * 系统商品分类管理
 * Class Goods
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/15 18:12
 */
class Cates extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'ShopCates';

    /**
     * 商品列表
     */
    public function index()
    {
        $this->title = '商品分类列表';

        $db = Db::name($this->table)
                ->order('sort asc,id asc');
        return parent::_list($db, false);
    }

    /**
     * 商品添加
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 商品编辑
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
        }
        $data = ToolsService::arr2table($data);
    }

     /**
     * 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            // 上级菜单处理
            $_menus = Db::name($this->table)->where(['is_display' => '1'])->order('sort asc,id asc')->select();
            $_menus[] = ['cname' => '顶级分类', 'id' => '0', 'pid' => '-1'];
            $menus = ToolsService::arr2table($_menus);
            foreach ($menus as $key => &$menu) {
                // 控制分类登记
                if (substr_count($menu['path'], '-') > 3) {
                    unset($menus[$key]);
                    continue;
                }
                if (isset($vo['pid'])) {
                    $current_path = "-{$vo['pid']}-{$vo['id']}";
                    if ($vo['pid'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
                        unset($menus[$key]);
                        continue;
                    }
                }
            }
            $this->assign('menus', $menus);
        }
    }

    /**
     * 删除分类
     */
    public function del()
    {
        $data = explode(',',$this->request->post('id'));
        if(!empty($data)){
            foreach ($data as $v) {
                // 查询该分类下是否存在商品
                $res = Db::name('shop_goods')
                            ->field('id')
                            ->where('cid',$v)
                            ->find();
                if(!empty($res)){
                    $cate = Db::name('shop_cates')
                            ->field('cname')
                            ->where('id',$v)
                            ->find();
                    $this->error($cate['cname']."下存在商品，无法删除！");
                }
            }
        }
        if (DataService::update($this->table)) {
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }

    /**
     * 分类禁用
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("分类禁用成功", '');
        }
        $this->error("分类禁用成功，请稍候再试！");
    }

    /**
     * 分类显示
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("分类显示成功", '');
        }
        $this->error("分类显示失败，请稍候再试！");
    }

}
